home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / Frontier / Frontier SDK 2.1 / Toolkits / Applet Toolkit / appletkb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-09  |  5.8 KB  |  305 lines  |  [TEXT/KAHL]

  1.  
  2. /*© Copyright 1988-1992 UserLand Software, Inc.  All Rights Reserved.*/
  3.  
  4.  
  5. #include "appletmemory.h"
  6. #include "appletkb.h"
  7.  
  8.  
  9.  
  10. #define keycodeclear 71 /*keycodes for numeric keybad*/
  11. #define keycodeminus 78
  12. #define keycodeplus 69
  13. #define keycodetimes 67
  14. #define keycodeseven 89
  15. #define keycodeeight 91
  16. #define keycodenine 92
  17. #define keycodedivide 77
  18. #define keycodefour 86
  19. #define keycodefive 87
  20. #define keycodesix 88
  21. #define keycodecomma 72
  22. #define keycodeone 83
  23. #define keycodetwo 84
  24. #define keycodethree 85
  25. #define keycodeenter 76
  26. #define keycodezero 82
  27. #define keycodeperiod 65
  28.  
  29.  
  30. tykeystrokerecord keyboardstatus; 
  31.  
  32. static boolean flescapepending = false;
  33.  
  34.  
  35.  
  36.  
  37. boolean arrowkey (char chkb) {
  38.     
  39.     /*
  40.     return true if the indicated key character is an arrow key.
  41.     */
  42.     
  43.     register char ch = chkb;
  44.     
  45.     return (
  46.         (ch == chuparrow) || (ch == chdownarrow) || 
  47.         
  48.         (ch == chleftarrow) || (ch == chrightarrow));
  49.     } /*arrowkey*/
  50.  
  51.  
  52. tydirection keystroketodirection (char ch) {
  53.     
  54.     switch (ch) {
  55.         
  56.         case chleftarrow:
  57.             return (left);
  58.             
  59.         case chrightarrow:
  60.             return (right);
  61.             
  62.         case chuparrow:
  63.             return (up);
  64.             
  65.         case chdownarrow:
  66.             return (down);
  67.         } /*switch*/
  68.         
  69.     return (nodirection);
  70.     } /*keystroketodirection*/
  71.     
  72.  
  73. static void kbsetstatus (EventRecord ev, tykeystrokerecord *kbs) {
  74.     
  75.     /*
  76.     12/11/90 dmb: don't count the caps lock as a modifier in ctmodifiers.
  77.     
  78.     2/21/91 dmb: handle calls for mousedown events.  we just want to 
  79.     record modifier keys
  80.     */
  81.     
  82.     register short ct;
  83.     register char chkb;
  84.     register short keycode;
  85.     tykeystrokerecord kbcurrent;
  86.     
  87.     kbcurrent.flshiftkey = (ev.modifiers & shiftKey) != 0;
  88.     
  89.     kbcurrent.flcmdkey = (ev.modifiers & cmdKey) != 0;
  90.     
  91.     kbcurrent.floptionkey = (ev.modifiers & optionKey) != 0;
  92.     
  93.     kbcurrent.flalphalock = (ev.modifiers & alphaLock) != 0;
  94.     
  95.     kbcurrent.flcontrolkey = (ev.modifiers & controlKey) != 0;
  96.     
  97.     ct = 0;
  98.     
  99.     if (kbcurrent.flshiftkey) ct++;
  100.     
  101.     if (kbcurrent.flcmdkey) ct++;
  102.     
  103.     if (kbcurrent.floptionkey) ct++;
  104.     
  105.     /*
  106.     if (kbcurrent.flalphalock) ct++;
  107.     */
  108.     
  109.     if (kbcurrent.flcontrolkey) ct++;
  110.     
  111.     kbcurrent.ctmodifiers = ct;    
  112.     
  113.     if (ev.what == mouseDown) {
  114.         
  115.         kbcurrent.chkb = chnul;
  116.         
  117.         kbcurrent.keycode = 0;
  118.         
  119.         kbcurrent.flautokey = false;
  120.         
  121.         kbcurrent.keydirection = nodirection;
  122.         }
  123.     else {
  124.         
  125.         kbcurrent.chkb = chkb = ev.message & charCodeMask; /*get the keystroke*/
  126.         
  127.         kbcurrent.flautokey = (ev.what == autoKey);
  128.         
  129.         kbcurrent.keycode = keycode = (ev.message & keyCodeMask) >> 8;
  130.         
  131.         kbcurrent.keydirection = keystroketodirection (chkb);
  132.         
  133.         if (kbcurrent.flcmdkey  &&  kbcurrent.floptionkey) {
  134.     
  135.             /*
  136.             we don't want to option character, so find the normal character from 
  137.             the keymap.  see IM V-195
  138.             */
  139.             
  140.             Handle hkchr;
  141.             long state = 0;
  142.             
  143.             if (hkchr = GetResource ('KCHR', 0))
  144.                 kbcurrent.chkb = KeyTrans (*hkchr, kbcurrent.keycode, &state) & 0x000000ff;
  145.             };
  146.     
  147.         kbcurrent.flkeypad = /*true if it is a keystroke from the numeric keypad*/
  148.             
  149.             (keycode == keycodeclear)         || (keycode == keycodeminus)     || 
  150.             
  151.             (keycode == keycodeplus)         || (keycode == keycodetimes)     ||
  152.             
  153.             (keycode == keycodeseven)         || (keycode == keycodeeight)     ||
  154.             
  155.             (keycode == keycodenine)         || (keycode == keycodedivide)     ||
  156.             
  157.             (keycode == keycodefour)         || (keycode == keycodefive)     ||
  158.             
  159.             (keycode == keycodesix)         || (keycode == keycodecomma)     ||
  160.             
  161.             (keycode == keycodeone)         || (keycode == keycodetwo)         ||
  162.             
  163.             (keycode == keycodethree)         || (keycode == keycodeenter)     ||
  164.             
  165.             (keycode == keycodezero)         || (keycode == keycodeperiod);
  166.         }
  167.     
  168.     *kbs = kbcurrent; /*set for caller*/
  169.     } /*kbsetstatus*/
  170.     
  171.  
  172. void setkeyboardstatus (EventRecord ev) {
  173.     
  174.     /*
  175.     sets a global that may be referenced by anyone.
  176.     
  177.     should be called every time a new event is received.
  178.     */
  179.     
  180.     kbsetstatus (ev, &keyboardstatus); /*use global*/
  181.     } /*setkeyboardstatus*/
  182.  
  183.  
  184. void keyboardclearescape (void) {
  185.     
  186.     flescapepending = false;
  187.     } /*keyboardclearescape*/
  188.  
  189.  
  190. void keyboardsetescape (void) {
  191.     
  192.     /*ouch (); /*audible feedback asap, multimedia!*/
  193.     
  194.     /*
  195.     shellfrontrootwindowmessage ("\pCancelled.");
  196.     */
  197.     
  198.     flescapepending = true;
  199.     } /*keyboardsetescape*/
  200.  
  201.  
  202. boolean keyboardescape (void) {
  203.     
  204.     /*
  205.     check to see if the user has pressed cmd-period.  if not, no effect on
  206.     the event queue and we return false.
  207.     
  208.     otherwise, we remove the keystroke and return true.  the caller is expected
  209.     to return quickly!
  210.     */
  211.     
  212.     register unsigned long tc;
  213.     tykeystrokerecord kbcurrent;
  214.     EventRecord ev;
  215.     static unsigned long lastcheck = 0;
  216.     
  217.     if (flescapepending)
  218.         return (true);
  219.     
  220.     tc = TickCount ();
  221.     
  222.     if ((tc - 60) < lastcheck) /*check 1 time per second*/
  223.         return (false);
  224.     
  225.     lastcheck = tc; /*remember for next time*/
  226.     
  227.     if (EventAvail (keyDownMask, &ev)) {
  228.         
  229.         kbsetstatus (ev, &kbcurrent);
  230.         
  231.         if (kbcurrent.flcmdkey && (kbcurrent.chkb == '.')) {
  232.             
  233.             GetNextEvent (keyDownMask, &ev); /*get rid of the cmd-period*/
  234.             
  235.             keyboardsetescape (); /*multimedia!*/
  236.             
  237.             return (true);
  238.             }
  239.         }
  240.     else { /*potentially surrender the processor to background tasks*/
  241.         
  242.         /*WaitNextEvent (nullEvent, &ev, 1, nil);*/
  243.         }
  244.     
  245.     /*motorsound ();*/
  246.     
  247.     return (false);
  248.     } /*keyboardescape*/
  249.  
  250.  
  251. void keyboardpeek (tykeystrokerecord *kbrecord) {
  252.     
  253.     register ptrkeystrokerecord p = kbrecord;
  254.     KeyMap keys;
  255.     
  256.     clearbytes (p, longsizeof (tykeystrokerecord));
  257.     
  258.     GetKeys (keys);
  259.     
  260.     (*p).flshiftkey = BitTst (&keys, 63);
  261.         
  262.     (*p).flcmdkey = BitTst (&keys, 48);
  263.         
  264.     (*p).floptionkey = BitTst (&keys, 61);
  265.         
  266.     (*p).flcontrolkey = BitTst (&keys, 60);
  267.     } /*keyboardpeek*/
  268.  
  269.  
  270. static boolean keydown (short keycode) {
  271.  
  272.     KeyMap keys;
  273.     
  274.     GetKeys (keys);
  275.     
  276.     return (BitTst (&keys, keycode));
  277.     } /*keydown*/
  278.     
  279.     
  280. boolean enterkeydown (void) {    
  281.     
  282.     return (keydown (75));
  283.     } /*enterkeydown*/
  284.     
  285.     
  286. boolean optionkeydown (void) {
  287.     
  288.     return (keydown (61));
  289.     } /*optionkeydown*/
  290.     
  291.     
  292. boolean cmdkeydown (void) {
  293.         
  294.     return (keydown (48));
  295.     } /*cmdkeydown*/
  296.     
  297.     
  298. boolean shiftkeydown (void) {
  299.         
  300.     return (keydown (63));
  301.     } /*shiftkeydown*/
  302.     
  303.     
  304.     
  305.